//
//  ViewController.m
//  20-XML-SAX And DOM
//
//  Created by dllo on 16/8/10.
//  Copyright © 2016年 LST. All rights reserved.
//

#import "ViewController.h"
#import "Student.h"
#import "GDataXMLNode.h"
@interface ViewController ()<NSXMLParserDelegate>
@property (nonatomic, strong)NSMutableArray *stuArray;
@property (nonatomic, strong)NSMutableArray *domStuArray;
@property (nonatomic, copy)NSString *currentElementName;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    // 第三方
    // XMLReader
    // 将XML结构转成Dic
}
#warning mark - SEX 解析
// XML 分为两种解析方式
// 第一种 SAX 逐行解析 - 使用系统的解析器 NSXMLParser
// 还可以判断是否为空, 然后在节点尾将_currentElementName赋为空, 让再次循环
- (IBAction)saxParse:(id)sender {
    NSString *path = [[NSBundle mainBundle] pathForResource:@"stu_xml" ofType:@"xml"];
    NSData *data = [NSData dataWithContentsOfFile:path];
    NSXMLParser *xmlparser = [[NSXMLParser alloc] initWithData:data];
    xmlparser.delegate = self;
    [xmlparser parse];
}
// 1.解析器开始
- (void)parserDidStartDocument:(NSXMLParser *)parser {
    self.stuArray = [NSMutableArray array];
}
// 2.遇到节点头 方法回调, 记录当前节点, 方便后续赋值
// 打印所有节点头
-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary<NSString *,NSString *> *)attributeDict {
    NSLog(@"节点头:%@", elementName);
    _currentElementName = elementName;
    if ([elementName isEqualToString:@"student"]) {
        Student *stu = [Student new];
        [_stuArray addObject:stu];
    }
}
// 3.节点的值, 根据节点内容决定值.没有值时对应的结果\n\t \n\t\t
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
    if (![string hasSuffix:@" "] && ![string hasSuffix:@"\n"]) {
        Student *stu = [_stuArray lastObject];
        [stu setValue:string forKey:_currentElementName];
        NSLog(@"节点的值:%@---结束", string);
    }
}
// 4.遇到节点尾, 方法的回调 , 判断当前节点的结束
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
    NSLog(@"节点尾:%@", elementName);
}
// 5.解析器结束
- (void)parserDidEndDocument:(NSXMLParser *)parser {
    NSLog(@"%@", _stuArray);
}
// 解析失败时, 调用
- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError {
    NSLog(@"解析失败:%@", parseError);
}
#warning mark - DOM解析
// 第二种解析方式, DOM解析
// 关于GDataXMLNode的引入
// 1.引入.h.m文件
// 2.引入系统动态库libxml2.tbd
// 3.根据GDataXMLNode头文件的提示, 添加
// 4.如果是ARC环境下, 需对文件进行MRC编译, 添加-fno-objc-arc
- (IBAction)domParse:(id)sender {
    self.domStuArray = [NSMutableArray array];
    NSString *SMLDatapath = [[NSBundle mainBundle]pathForResource:@"stu_xml" ofType:@"xml"];
    NSData *data = [NSData dataWithContentsOfFile:SMLDatapath];
    NSError *error = nil;
    // 1.创建 GDataXMLDocument对象
    // 根据XML的Data的数据生成一个树形结构的Document
    GDataXMLDocument *document = [[GDataXMLDocument alloc] initWithData:data options:0 error:&error];
    // 2.根据Document获取根节点
    GDataXMLElement *rootElement = [document rootElement];
    NSLog(@"%@", rootElement.name);
    // 3.根据根节点获取子节点数组
    NSArray *rootChildElements = rootElement.children;
    
    // 4.遍历子节点数组
    for (GDataXMLElement *childElement in rootChildElements) {
        // 5.对应所有的student节点
        Student *stu = [Student new];
        [self.domStuArray addObject:stu];
        // 6.遍历student的所有子节点
        for (GDataXMLElement *grandSonElement in childElement.children) {
            // 7. 赋值
            [stu setValue:grandSonElement.stringValue forKey:grandSonElement.name];
        }
    }
    NSLog(@"%@", self.domStuArray);
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
